package scau.cn.component.domain;

import java.io.File;
import java.util.ArrayList;

import scau.cn.component.abstracts.Disk;
import scau.cn.fat.FAT;

public class Catalog extends Disk {
	// 每个目录下最大的文件数
	final int maxNumber = 8;
	// 当前目录下的文件数
	private int presentNumber = 0;
	// 目录下的目录或者文件的串
	private ArrayList<Disk> component = new ArrayList<>(8);
	
	//-------------------------------构造器--------------------------------------------
	public Catalog(int nameAndLength, short type, byte attribute, byte start, FAT fat, Disk parent) {
		this.nameAndLength = nameAndLength;
		this.type = type;
		if(parent != null && parent.getOpType() == 0) {
			//当父母不为空，且父母为只读，那么该目录也只能只读
			//即该目录属性为父母的目录属性
			this.attribute = parent.getAttribute();
		} else {
			this.attribute = attribute;
		}
		this.start = start;
		this.fat = fat;
		this.parent = parent;
	}
	
	public Catalog(String name, String type, byte attribute, FAT fat, Disk parent) throws Exception {
		this.nameAndLength = 0;
		this.setName(name);
		this.nameAndLength += 1;
		this.setTypeToDisk(type);
		System.out.println("Catalog: " + attribute);
		if(parent != null && parent.getOpType() == 0) {
			//当父母不为空，且父母为只读，那么该目录也只能只读
			//即该目录属性为父母的目录属性
			this.attribute = parent.getAttribute();
		} else {
			this.attribute = attribute;
		}
		System.out.println("Catalog: " + this.attribute);
		this.start = fat.findFree();
		this.fat = fat;
		this.parent = parent;
	}
	
	public Catalog() {}
	
	/**
	 * @param nameAndLength 名字和长度
	 * @return 找到该文件名的情况
	 */
	public boolean findFileName(int nameAndLength) {
		for (Disk disk : component) {
			if (disk instanceof FileN) {
				int name = disk.getNameAndLength() >> 8;
				int names = nameAndLength >> 8;
				System.out.println(name+" "+disk.getName());
				System.out.println(names+" "+disk.getName());
				System.out.println((name&names)==name);
				if ((name - names) == 0) {
				
					return true;
				}
			}
		}
		return false;
	}
	
	/**
	 * @param name 目录名字
	 * @return 找到该目录下是否存在名字为Name的名录
	 */
	public boolean findFileName(String Name) {
		for (Disk disk : component) {
			if(disk instanceof Catalog) {
				if(disk.getName().equals(Name)) {
					return true;
				}
			}
		}
		return false;
	}
	
	/**
	 * 寻找文件：
	 * 		在当前目录下寻找文件（不用递归找，只找当前目录，若还有子目录也不必寻找）
	 * @param fileName ： 文件名字
	 * @return file ： File
	 * @author 严浩强
	 */
	public FileN getFileFromComponent(String fileName) {
		for(int i = 0; i < component.size(); i++) {
			if(component.get(i) instanceof FileN) {
				FileN file = (FileN) component.get(i);
				if(fileName.equals(file.getName())) {
					return file;
				}
			}
		}
		return null;
	}
	
	/**
	 * 寻找目录：
	 * 		在当前目录下寻找文件（不用递归找，只找当前目录，若还有子目录也不必寻找）
	 * @param fileName ： 目录名字
	 * @return file ： Catalog
	 * @author 严浩强
	 */
	public Catalog getCatalogFromComponent(String Name) {
		for(int i = 0; i < component.size(); i++) {
			if(component.get(i) instanceof Catalog) {
				Catalog file = (Catalog) component.get(i);
				if(Name.equals(file.getName())) {
					return file;
				}
			}
		}
		return null;
	}
	
	public Catalog getCatalogFromRoot(String Path) {
		return null;
	}
	
	
	public boolean addComponent(Disk disk) {
		if (this.getPresentNumber() == 8) {
			return false;
		} else {
			this.component.add(disk);
			this.presentNumber++;
			return true;
		}
	}

	public boolean deleteComponent(Disk disk) {
		if (this.getPresentNumber() == 0) {
			return false;
		} else {
			this.component.remove(disk);
			this.presentNumber--;
			return true;
		}
	}


	public ArrayList<Disk> getComponent() {
		return component;
	}

	public void setComponent(ArrayList<Disk> component) {
		this.component = component;
	}

	public int getPresentNumber() {
		return presentNumber;
	}

	public void setPresentNumber(int presentNumber) {
		this.presentNumber = presentNumber;
	}

	@Override
	public boolean modify() {
		// TODO Auto-generated method stub
		return false;
	}
	
	/**
	 * 删除目录：
	 * 		
	 * 2019/11/25 17:08 修改  严浩强
	 */
	@Override
	public boolean delete() {
		System.out.println(this.getName() + " " + this.getStart() + " " + this.fat.getStatus(this.getStart()));
		//这个是能递归删除的
		ArrayList<Disk> components = new ArrayList<>();
		for(Disk disk : component) {
			components.add(disk);
		}
		for (Disk disk : components) {
			System.out.println("delete " + disk.findPath());
			disk.delete();
		}
		//删除其在父目录中ArrayList中的记录
		Catalog parent = (Catalog) this.getParent();
		parent.getComponent().remove(this);
		File file = new File(this.findPath());
		//File的delete函数规定了如果是为非空目录的话就必须先删除其目录下的东西才能删除
		//但我们这个函数本身就是递归的删除
		System.out.println(this.findPath() + " " + file.delete());
		//修改为空闲 目录只占一个FAT
		fat.setStatus(start, 0);
		return true;
	}

}
